package com.lukk.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.constant.MerchantWalletRecordType;
import com.lukk.constant.PayMethod;
import com.lukk.dao.MerchantDAO;
import com.lukk.dao.MerchantsWalletRecordDAO;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.MerchantsWalletRecord;
import com.lukk.service.IMerchantsWalletRecordService;
import com.lukk.util.DateUtils;
import com.lukk.util.LuKeOrderNoUtil;
import com.lukk.util.jpush.JPushUtil;

/**
 * 
 * MerchantsWalletRecordServiceImpl 上午10:25:21
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Service("merchantsWalletRecordService")
public class MerchantsWalletRecordServiceImpl implements IMerchantsWalletRecordService {
	@Resource
	MerchantsWalletRecordDAO merchantsWalletRecordDAO;
	@Resource
	MerchantDAO merchantDAO;

	@Override
	public boolean insertPayMoreMoneyRecord(String cause, Integer indentID, Integer merchantID, Integer workerID,
			Double moreMoney) {
		MerchantsWalletRecord merchantsWalletRecord = new MerchantsWalletRecord();
		merchantsWalletRecord.setMainMoney(moreMoney);
		merchantsWalletRecord.setMerchantsId(merchantID);
		merchantsWalletRecord.setIndentId(indentID);
		merchantsWalletRecord.setRemark(cause);
		merchantsWalletRecord.setType(5);
		merchantsWalletRecord.setOrderNo(LuKeOrderNoUtil.getMerchantsOrderNo(merchantID, PayMethod.OTHERPAY.getIndex(),
				MerchantWalletRecordType.PRICE_DIFFERENCE.getIndex()));
		merchantsWalletRecord.setRecordBeginTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		merchantsWalletRecordDAO.insertPayMoreMoneyRecord(merchantsWalletRecord);
		String alert = "补差价提醒";
		String msgContent = "你有需要补的差价，请及时处理";
		Map<String, String> extras = new HashMap<String, String>();
		extras.put("from", "JPush");
		extras.put("msgType", "3");
		extras.put("msgTitle", alert);
		JPushUtil.pushDialogByOneAlias(alert, msgContent, extras, merchantDAO.selectByPrimaryKey(merchantID).getImei());
		return false;
	}

	@Override
	public Map<String, Object> selectMerchantWalletRecordByMechant(Integer offset, Integer limit, String sort,
			String order, Integer indentID, String beginTime, String endTime, Integer payStatus, HttpSession session) {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		// 查询出来每一个员工所分配的商人的总数
		queryMap.put("offset", offset);
		queryMap.put("limit", limit);
		queryMap.put("sort", sort);
		queryMap.put("order", order);
		queryMap.put("payStatus", payStatus);
		queryMap.put("beginTime", beginTime);
		queryMap.put("endTime", endTime);
		queryMap.put("indentID", indentID);
		queryMap.put("internalWorkerID", internalWorker.getId());
		List<MerchantsWalletRecord> beans = merchantsWalletRecordDAO.selectMerchantWalletRecordByMechant(queryMap);
		retsMap.put("total", merchantsWalletRecordDAO.selectMerchantWalletRecordCountByMechant(queryMap));
		retsMap.put("rows", beans);
		return retsMap;
	}
}
